package com.yuju.scheduler.service.task;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.yuju.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.yuju.account.enums.AccountStatusEnum;
import com.yuju.account.enums.AccountTypeEnum;
import com.yuju.account.enums.BusinessTypeDetailEnum;
import com.yuju.account.enums.BusinessTypeEnum;
import com.yuju.account.service.IAccountService;
import com.yuju.enums.CheckinTypeEnum;
import com.yuju.enums.StatusEnum;
import com.yuju.group.model.HotelModel;
import com.yuju.group.service.IHotelService;
import com.yuju.group.service.impl.HotelServiceImpl;
import com.yuju.login.model.OpModel;
import com.yuju.login.service.IOpService;
import com.yuju.model.account.AccountModel;
import com.yuju.order.enums.RoomOrderSettleStatusEnum;
import com.yuju.order.enums.RoomOrderStatusEnum;
import com.yuju.order.model.RoomOrderModel;
import com.yuju.order.service.IRoomOrderService;

/**
 * 
 * 类名：TimingEnterAccounts  <br />
 *
 * 功能：滚房费
 *
 * @author : zhousc <br />
 * @Date : 2017年12月18日 上午10:31:07  <br />
 * @version : 1.1 <br />
 */
@Component
public class TimingEnterAccounts {

	@Autowired
	private IRoomOrderService roomOrderService;
	@Autowired
	private IAccountService accountService;
	@Autowired
	private IOpService opService;
	@Autowired
	private IHotelService hotelService;
	
	/**
	 * 
	 * 功能：短租房过房费<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月18日 上午10:34:52 <br/>
	 */
	@Scheduled(cron="0 0 0 * * ?")
	public void dayRoomEnterAccounts() {
		//查询所有的在住订单
		RoomOrderModel roomOrderModel = new RoomOrderModel();
		roomOrderModel.setCheckinType((byte)CheckinTypeEnum.CHECKIN_QT.getValue());
		roomOrderModel.setCheckinStatus(RoomOrderStatusEnum.CHECKIN.getValue());
		roomOrderModel.setSettleStatus(RoomOrderSettleStatusEnum.WEIJIE.getValue());
		//查询  在住，未结状态的 全天房
		List<RoomOrderModel> roomOrderList = roomOrderService.list(roomOrderModel);
		
		//为每个订单添加一条全日租房费的账务
		List<AccountModel> accountModels = new ArrayList<>();
		for (int i = 0; i < roomOrderList.size(); i++) {
			AccountModel accountModel = new AccountModel();
			//房间订单ID
			accountModel.setRoomOrderId(roomOrderList.get(i).getId());
			//分店ID
			accountModel.setHotelId(roomOrderList.get(i).getHotelId());
			//需要过得房费金额
			accountModel.setMoney(roomOrderList.get(i).getRoomPrice());
			//数量
			accountModel.setAmount(1);
			//主宾客ID
			accountModel.setRoomGuestId(roomOrderList.get(i).getMainGuestId());
			//集团ID
			accountModel.setGroupId(roomOrderList.get(i).getGroupId());
			//账务类型 ：应收
			accountModel.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_YS.getValue());
			//账务状态：OK
			accountModel.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
			accountModel.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
			//营业项目
			accountModel.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_YSFF.getValue());
			accountModel.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_YSFF.getName());
			//营业类型
			accountModel.setBusinessTypeDetail(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QRZ.getValue());
			accountModel.setBusinessTypeDetailName(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QRZ.getName());
			//营业详情
			accountModel.setBusinessDetail(BusinessTypeEnum.BUSINESS_TYPE_YSFF.getName()+BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QRZ.getName());
			//添加分店CODE
			HotelModel findById = hotelService.findById(roomOrderList.get(i).getHotelId());
			accountModel.setHotelCode(findById.getCode());
			//根据集团ID查询集团超级管理员账号ID	
			OpModel opModel = new OpModel(); 
			opModel.setGroupId(roomOrderList.get(i).getGroupId());
			opModel.setIsAdmin(StatusEnum.TRUE.getValue());
			OpModel find = opService.find(opModel);
			accountModel.setCreateUserId(find.getEmpId());
			//添加到账务集合，循环添加完成后 统一添加
			accountModels.add(accountModel);
		}
		//调用账务新增接口 进行账务新增
		for(AccountModel model:accountModels){
			try {
				accountService.saveOrderAccount(model);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}

	private int getAmendDayCount(Date day) {
		//零点滚房费时，当前日和入住日一致。
		Date checkinTime = day;
		if (checkinTime == null) {
			return 0;
		}

		Calendar calCheckinTime = Calendar.getInstance();
		calCheckinTime.setTime(checkinTime);
		calCheckinTime.set(Calendar.HOUR_OF_DAY, 0);
		calCheckinTime.set(Calendar.MINUTE, 0);
		calCheckinTime.set(Calendar.SECOND, 0 );
		calCheckinTime.set(Calendar.MILLISECOND, 0);

		Calendar calToday = Calendar.getInstance();
		calToday.set(Calendar.HOUR_OF_DAY, 0);
		calToday.set(Calendar.MINUTE, 0);
		calToday.set(Calendar.SECOND, 0 );
		calToday.set(Calendar.MILLISECOND, 0);

		//入住时间向后循环，当等于当天时，则计算最近一个月的差价
		Calendar calPreMonth = Calendar.getInstance();
		calPreMonth.setTime(calCheckinTime.getTime());
		for (int i = 0; i < 36; i++) {
			calPreMonth.add(Calendar.MONTH, 1);
			if (calPreMonth.after(calToday)) {
				break;
			}

			if (calPreMonth.get(Calendar.YEAR) == calToday.get(Calendar.YEAR)
					&& calPreMonth.get(Calendar.MONTH) == calToday.get(Calendar.MONTH)
					&& calPreMonth.get(Calendar.DATE) == calToday.get(Calendar.DATE)) {
				//当日上一月
				calPreMonth.add(Calendar.MONTH, -1);
				int dayCount = (int) ((calToday.getTimeInMillis() - calPreMonth.getTimeInMillis()) / (1000*3600*24));
				return dayCount;
			}
		}

		return 0;
	}

	private BigDecimal getAmendPrice(RoomOrderModel roomOrderModel) {
		//零点滚房费时，当前日和入住日一致。
		int dayCount = getAmendDayCount(roomOrderModel.getCheckinTime());
		if (dayCount == 0) {
			return null;
		}

		BigDecimal dayPrice = roomOrderModel.getRoomPrice();
		BigDecimal monthPrice = roomOrderModel.getMonthPrice();
		if (dayPrice == null || monthPrice == null) {
			return null;
		}

		BigDecimal amendPrice = monthPrice.subtract(dayPrice.multiply(BigDecimal.valueOf(dayCount)));

		return amendPrice;
	}

	
	/**
	 * 
	 * 功能：公寓房过房费<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月18日 上午10:34:52 <br/>
	 */
	@Scheduled(cron="0 0 0 * * ?")
	public void apartmentRoomEnterAccounts() {
		//查询所有的在住订单
		RoomOrderModel roomOrderModel = new RoomOrderModel();
		roomOrderModel.setCheckinType((byte)CheckinTypeEnum.CHECKIN_GY.getValue());
		roomOrderModel.setCheckinStatus(RoomOrderStatusEnum.CHECKIN.getValue());
		roomOrderModel.setSettleStatus(RoomOrderSettleStatusEnum.WEIJIE.getValue());
		//查询  在住，未结状态的 公寓房
		List<RoomOrderModel> roomOrderList = roomOrderService.list(roomOrderModel);
		
		//为每个订单添加一条全日租房费的账务
		List<AccountModel> accountModels = new ArrayList<>();
		for (int i = 0; i < roomOrderList.size(); i++) {
			AccountModel accountModel = new AccountModel();
			//房间订单ID
			accountModel.setRoomOrderId(roomOrderList.get(i).getId());
			//分店ID
			accountModel.setHotelId(roomOrderList.get(i).getHotelId());
			//需要过得房费金额
			accountModel.setMoney(roomOrderList.get(i).getRoomPrice());
			//数量
			accountModel.setAmount(1);
			//主宾客ID
			accountModel.setRoomGuestId(roomOrderList.get(i).getMainGuestId());
			//集团ID
			accountModel.setGroupId(roomOrderList.get(i).getGroupId());
			//账务类型 ：应收
			accountModel.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_YS.getValue());
			//账务状态：OK
			accountModel.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
			accountModel.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
			//营业项目
			accountModel.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_YSFF.getValue());
			accountModel.setBusinessTypeName(BusinessTypeEnum.BUSINESS_TYPE_YSFF.getName());
			//营业类型
			accountModel.setBusinessTypeDetail(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QRZ.getValue());
			accountModel.setBusinessTypeDetailName(BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QRZ.getName());
			//营业详情
			accountModel.setBusinessDetail(BusinessTypeEnum.BUSINESS_TYPE_YSFF.getName()+BusinessTypeDetailEnum.BUSINESS_TYPE_DETAIL_QRZ.getName());
			//添加分店CODE
			HotelModel findById = hotelService.findById(roomOrderList.get(i).getHotelId());
			accountModel.setHotelCode(findById.getCode());
			
			//根据集团ID查询集团超级管理员账号ID	
			OpModel opModel = new OpModel(); 
			opModel.setGroupId(roomOrderList.get(i).getGroupId());
			opModel.setIsAdmin(StatusEnum.TRUE.getValue());
			OpModel find = opService.find(opModel);
			accountModel.setCreateUserId(find.getEmpId());
			//添加到账务集合，循环添加完成后 统一添加
			accountModels.add(accountModel);

			//公寓房到月房价补齐
			BigDecimal amendPrice = getAmendPrice(roomOrderList.get(i));
			if (amendPrice != null) {
				accountModel.setMoney(amendPrice);
				//营业详情
				accountModel.setBusinessDetail("整月房费补齐");
				accountModels.add(accountModel);
			}
		}
		//调用账务新增接口 进行账务新增
		for(AccountModel model:accountModels){
			try {
				accountService.saveOrderAccount(model);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}
}
